<?php
// 应用公共文件
/** 
* @desc 通过curl请求接口
* @param str $url 接口地址 
* @param data $params 数据 
* @param boolean $ispost 请求类型 
*/
function getCurl($url, $params = false, $ispost = 0){
	if(empty($url)){
		echo "cURL Error: url不能为空";
		return false;
	}
	$httpInfo = array();
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); // 默认值，让 cURL 自己判断使用哪个版本。 (强制使用 HTTP/1.1)。
	curl_setopt($ch, CURLOPT_USERAGENT, 'JuheData'); // 在HTTP请求中包含一个"User-Agent: "头的字符串。
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); // 在尝试连接时等待的秒数。设置为0，则无限等待。
	curl_setopt($ch, CURLOPT_TIMEOUT, 60);  // 设置超时限制防止死循环
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 要求结果保存到字符串中还是输出到屏幕上
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 爬取重定向页面
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($ch, CURLOPT_SSLVERSION, false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	if ($ispost) {
		curl_setopt($ch, CURLOPT_POST, true); // 发送一个常规的Post请求
		curl_setopt($ch, CURLOPT_POSTFIELDS,$params); // Post提交的数据包
		curl_setopt($ch, CURLOPT_URL, $url); // 设置URL
	} else {
		// GET请求，组装url
		if ($params) {
			curl_setopt($ch, CURLOPT_URL, $url.'?'.$params);
		} else {
			curl_setopt($ch, CURLOPT_URL, $url);
		}
	}
	$response = curl_exec($ch); // 运行cURL，请求URL，把结果复制给变量
	if ($response === FALSE) {
		echo "cURL Error: " . curl_error($ch); //捕抓异常
		return false;
	}
	$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 获取一个cURL连接资源句柄的信息
	$httpInfo = array_merge($httpInfo, curl_getinfo($ch));
	curl_close($ch);
	return $response;
}
/**
* 统一获取时间戳方法
*/
function getTimeInt(){
	return time()+ C('TIME_ZONE_OFFSET')*3600;
}
/**
 * 取某天零点时间戳(本地时间)
 * @param number $time 欲获取当日的时间戳
 * return number 这天零点时间戳
 */
function getZeroTime($time){
	// Z 时差偏移量的秒数。UTC 西边的时区偏移量总是负的，UTC 东边的时区偏移量总是正的。
	$timezone = date('Z', $time);
	$time += $timezone;
	$time = $time - $time % 86400 - $timezone;
	return $time;
}
/**
 * 递归删除目录和文件
 * @param string $dir_name
 * @return bool
 */
function delete_dir_file($dir_name){
	$result = false;
	if (is_dir($dir_name)) {
		if ($handle = opendir($dir_name)) {
			while (false !== ($item = readdir($handle))) {
				if ($item != '.' && $item != '..') {
					if (is_dir($dir_name . DIRECTORY_SEPARATOR . $item)) {
						delete_dir_file($dir_name . DIRECTORY_SEPARATOR . $item);
					} else {
						unlink($dir_name . DIRECTORY_SEPARATOR . $item);
					}
				}
			}
			closedir($handle);
			if (rmdir($dir_name)) {
				$result = true;
			}
		}
	}
	return $result;
}
/**
 * 递归处理菜单数据
 */
function gettreeitems($items){
	$tree = array();
	foreach ($items as $item) {
		if(isset($items[$item['pid']])){
			$items[$item['pid']]['children'][] = &$items[$item['mid']];
		}else{
			$tree[] = &$items[$item['mid']];
		}
	}
	return $tree;
}
/**
 * 递归处理菜单数据
 */
function formatMenus($items){
	foreach($items as &$item){
		if(isset($item['children'])){
			$item['children'] = array_values($item['children']);
			formatMenus($item['children']);
		}
	}
	$items = array_values($items);
	return $items;
}
function set_status_header($code = 200, $text = ''){
	$stati = array(
		200 => 'OK',
		201 => 'Created',
		202 => 'Accepted',
		203 => 'Non-Authoritative Information',
		204 => 'No Content',
		205 => 'Reset Content',
		206 => 'Partial Content',

		300 => 'Multiple Choices',
		301 => 'Moved Permanently',
		302 => 'Found',
		304 => 'Not Modified',
		305 => 'Use Proxy',
		307 => 'Temporary Redirect',

		400 => 'Bad Request',
		401 => 'Unauthorized',
		403 => 'Forbidden',
		404 => 'Not Found',
		405 => 'Method Not Allowed',
		406 => 'Not Acceptable',
		407 => 'Proxy Authentication Required',
		408 => 'Request Timeout',
		409 => 'Conflict',
		410 => 'Gone',
		411 => 'Length Required',
		412 => 'Precondition Failed',
		413 => 'Request Entity Too Large',
		414 => 'Request-URI Too Long',
		415 => 'Unsupported Media Type',
		416 => 'Requested Range Not Satisfiable',
		417 => 'Expectation Failed',

		500 => 'Internal Server Error',
		501 => 'Not Implemented',
		502 => 'Bad Gateway',
		503 => 'Service Unavailable',
		504 => 'Gateway Timeout',
		505 => 'HTTP Version Not Supported'
	);

	if ($code == '' OR !is_numeric($code)) {
		show_error('Status codes must be numeric', 500);
	}
	if (isset($stati[$code]) AND $text == '') {
		$text = $stati[$code];
	}
	if ($text == '') {
		show_error('No status text available.  Please check your status code number or supply your own message text.', 500);
	}
	$server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
	if (substr(php_sapi_name(), 0, 3) == 'cgi') {
		header("Status: {$code} {$text}", TRUE);
	} elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0') {
		header($server_protocol . " {$code} {$text}", TRUE, $code);
	} else {
		header("HTTP/1.1 {$code} {$text}", TRUE, $code);
	}
}
/**
* 在文本里加反斜杠
*/
function enSql($str){
	return addslashes($str);
}
/**
* 去掉等于号的BASE64
* @param mixed $sStr
*/
function b64($sStr){
	return str_replace("=","",base64_encode($sStr));
}

//判断字符串是否存在某值
function instr($str, $a){
	$r = stristr($str, $a);
	if (empty($r)) {
		return false;
	} else {
		return true;
	}
}

function is_weixin(){
	if (instr($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') || instr($_SERVER['HTTP_USER_AGENT'], 'weixin') || instr($_SERVER['HTTP_USER_AGENT'], 'wechat')) {
		return true;
	}
	return false;
}

//获取汉字的首字母
function getFirstCharters($str)
{
	if (empty($str)) {
		return '';
	}
	//取出参数字符串中的首个字符
	$temp_str = substr($str,0,1);
	if(ord($temp_str) > 127){
		$str = substr($str,0,3);
	}else{
		$str = $temp_str;
		$fchar = ord($str{0});
		if ($fchar >= ord('A') && $fchar <= ord('z')){
		  return strtoupper($temp_str);
		}else{
		  return null;
		}
	}
	$s1 = iconv('UTF-8', 'gb2312//IGNORE', $str);
	if(empty($s1)){
		return null;
	}
	$s2 = iconv('gb2312', 'UTF-8', $s1);
	if(empty($s2)){
		return null;
	}
	$s = $s2 == $str ? $s1 : $str;
	$asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
	if ($asc >= -20319 && $asc <= -20284)
	return 'A';
	if ($asc >= -20283 && $asc <= -19776)
	return 'B';
	if ($asc >= -19775 && $asc <= -19219)
	return 'C';
	if ($asc >= -19218 && $asc <= -18711)
	return 'D';
	if ($asc >= -18710 && $asc <= -18527)
	return 'E';
	if ($asc >= -18526 && $asc <= -18240)
	return 'F';
	if ($asc >= -18239 && $asc <= -17923)
	return 'G';
	if ($asc >= -17922 && $asc <= -17418)
	return 'H';
	if ($asc >= -17417 && $asc <= -16475)
	return 'J';
	if ($asc >= -16474 && $asc <= -16213)
	return 'K';
	if ($asc >= -16212 && $asc <= -15641)
	return 'L';
	if ($asc >= -15640 && $asc <= -15166)
	return 'M';
	if ($asc >= -15165 && $asc <= -14923)
	return 'N';
	if ($asc >= -14922 && $asc <= -14915)
	return 'O';
	if ($asc >= -14914 && $asc <= -14631)
	return 'P';
	if ($asc >= -14630 && $asc <= -14150)
	return 'Q';
	if ($asc >= -14149 && $asc <= -14091)
	return 'R';
	if ($asc >= -14090 && $asc <= -13319)
	return 'S';
	if ($asc >= -13318 && $asc <= -12839)
	return 'T';
	if ($asc >= -12838 && $asc <= -12557)
	return 'W';
	if ($asc >= -12556 && $asc <= -11848)
	return 'X';
	if ($asc >= -11847 && $asc <= -11056)
	return 'Y';
	if ($asc >= -11055 && $asc <= -10247)
	return 'Z';

	return rare_words($asc);
}
//百家姓中的生僻字
function rare_words($asc=''){
	$rare_arr = array(
		-3652=>array('word'=>"窦",'first_char'=>'D'),
		-8503=>array('word'=>"奚",'first_char'=>'X'),
		-9286=>array('word'=>"酆",'first_char'=>'F'),
		-7761=>array('word'=>"岑",'first_char'=>'C'),
		-5128=>array('word'=>"滕",'first_char'=>'T'),
		-9479=>array('word'=>"邬",'first_char'=>'W'),
		-5456=>array('word'=>"臧",'first_char'=>'Z'),
		-7223=>array('word'=>"闵",'first_char'=>'M'),
		-2877=>array('word'=>"裘",'first_char'=>'Q'),
		-6191=>array('word'=>"缪",'first_char'=>'M'),
		-5414=>array('word'=>"贲",'first_char'=>'B'),
		-4102=>array('word'=>"嵇",'first_char'=>'J'),
		-8969=>array('word'=>"荀",'first_char'=>'X'),
		-4938=>array('word'=>"於",'first_char'=>'Y'),
		-9017=>array('word'=>"芮",'first_char'=>'R'),
		-2848=>array('word'=>"羿",'first_char'=>'Y'),
		-9477=>array('word'=>"邴",'first_char'=>'B'),
		-9485=>array('word'=>"隗",'first_char'=>'K'),
		-6731=>array('word'=>"宓",'first_char'=>'M'),
		-9299=>array('word'=>"郗",'first_char'=>'X'),
		-5905=>array('word'=>"栾",'first_char'=>'L'),
		-4393=>array('word'=>"钭",'first_char'=>'T'),
		-9300=>array('word'=>"郜",'first_char'=>'G'),
		-8706=>array('word'=>"蔺",'first_char'=>'L'),
		-3613=>array('word'=>"胥",'first_char'=>'X'),
		-8777=>array('word'=>"莘",'first_char'=>'S'),
		-6708=>array('word'=>"逄",'first_char'=>'P'),
		-9302=>array('word'=>"郦",'first_char'=>'L'),
		-5965=>array('word'=>"璩",'first_char'=>'Q'),
		-6745=>array('word'=>"濮",'first_char'=>'P'),
		-4888=>array('word'=>"扈",'first_char'=>'H'),
		-9309=>array('word'=>"郏",'first_char'=>'J'),
		-5428=>array('word'=>"晏",'first_char'=>'Y'),
		-2849=>array('word'=>"暨",'first_char'=>'J'),
		-7206=>array('word'=>"阙",'first_char'=>'Q'),
		-4945=>array('word'=>"殳",'first_char'=>'S'),
		-9753=>array('word'=>"夔",'first_char'=>'K'),
		-10041=>array('word'=>"厍",'first_char'=>'S'),
		-5429=>array('word'=>"晁",'first_char'=>'C'),
		-2396=>array('word'=>"訾",'first_char'=>'Z'),
		-7205=>array('word'=>"阚",'first_char'=>'K'),
		-10049=>array('word'=>"乜",'first_char'=>'N'),
		-10015=>array('word'=>"蒯",'first_char'=>'K'),
		-3133=>array('word'=>"竺",'first_char'=>'Z'),
		-6698=>array('word'=>"逯",'first_char'=>'L'),
		-9799=>array('word'=>"俟",'first_char'=>'Q'),
		-6749=>array('word'=>"澹",'first_char'=>'T'),
		-7220=>array('word'=>"闾",'first_char'=>'L'),
		-10047=>array('word'=>"亓",'first_char'=>'Q'),
		-10005=>array('word'=>"仉",'first_char'=>'Z'),
		-3417=>array('word'=>"颛",'first_char'=>'Z'),
		-6431=>array('word'=>"驷",'first_char'=>'S'),
		-7226=>array('word'=>"闫",'first_char'=>'Y'),
		-9293=>array('word'=>"鄢",'first_char'=>'Y'),
		-6205=>array('word'=>"缑",'first_char'=>'G'),
		-9764=>array('word'=>"佘",'first_char'=>'S'),
		-9818=>array('word'=>"佴",'first_char'=>'N'),
		-9509=>array('word'=>"谯",'first_char'=>'Q'),
		-3122=>array('word'=>"笪",'first_char'=>'D'),
		-9823=>array('word'=>"佟",'first_char'=>'T'),
	);
	if(array_key_exists($asc, $rare_arr) && $rare_arr[$asc]['first_char']){
		return $rare_arr[$asc]['first_char'] ;
	}else{
		return null;
	}
}